package lcr;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Test0060 {
}


class Solution0060_1 {
    public int[] topKFrequent(int[] nums, int k) {
        int[] ans = new int[k];
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int num : nums) map.put(num, map.getOrDefault(num,0)+1);

        PriorityQueue<Integer> queue = new PriorityQueue<>((a, b)->map.get(b)-map.get(a));
        for (Integer i : map.keySet()) queue.offer(i);
        for (int i = 0; i < k; i++) ans[i] = queue.poll();
        return ans;
    }
}